import { Component,OnInit,ViewChild } from '@angular/core';
import { CommonService } from '../service/common.service'
import { Router } from '@angular/router'
import { EventService } from '../service/event.service'
import { StorageService } from '../service/storage.service'

@Component({
  selector: 'app-tab3',
  templateUrl: 'tab3.page.html',
  styleUrls: ['tab3.page.scss']
})
export class Tab3Page {
  @ViewChild('slidesa') slidesa;

  public segmentAModel:string = 's1';

  public userInfo:any

  public fildetails:any
  public xchdetails:any
  public bzzdetails:any

  public isqbfil:string = ''
  public isqbxch:string = ''
  
  
  public incometoday:any = {
    fil:{},
    xch:{},
    bzz:{}
  }
  constructor(private router:Router,private common:CommonService,private eventService:EventService,private storage:StorageService) {}

  ngOnInit(){
    this.userInfo = this.common.getInfo()

    this.getPoolData('fil')
    this.incomeToday('fil')

    this.getPoolData('xch')
    this.incomeToday('xch')
    
    this.getPoolData('bzz')
    this.incomeToday('bzz')

    //监听 通知数量 事件  
    this.eventService.event.on('allearnings',()=>{
      this.getPoolData('fil')
      this.incomeToday('fil')

      this.getPoolData('xch')
      this.incomeToday('xch')
      
      this.getPoolData('bzz')
      this.incomeToday('bzz')
      // console.log('算力更新')
    })

    //监听 信息缓存 事件
    this.eventService.event.on('userstate',()=>{
      // 传ID更新储存
      var api = '/tbr/getUserInfo'
      var params = {'userId': this.common.getInfo().userId}
      
      this.common.post(api,params,false).then((response:any)=>{
        if(response.code == 200){
          // 保存信息
          this.storage.set('PersonalInfo',response.data)
          this.userInfo = this.common.getInfo()
        }
      })
    })

    //监听 信息缓存 事件
    this.eventService.event.on('userfile',()=>{
      console.log('更新fil')
    })
  }


  // 切换// 切换
  ontabs(value:any){
    let index:any;
    if(value == 's1'){
      index = 0
    }
    if(value == 's2'){
      index = 1
    }
    if(value == 's3'){
      index = 2
    }
    this.slidesa.slideTo(index,300)
  }
  // 滑动切换
  slideChange(){
    this.slidesa.getActiveIndex().then( n =>{
      // console.log(Number)
      if(n == 0){
        this.segmentAModel = 's1'
      }
      if(n == 1){
        this.segmentAModel = 's2'
      }
      if(n == 2){
        this.segmentAModel = 's3'
      }
    })
  }


  // 刷新
  doRefresh(event) {
    setTimeout(() => {
      this.common.getIdInfo()
      this.ngOnInit()
      event.target.complete();
    }, 1500);
  }

  // 今日收益
  incomeToday(arg){
    var api = '/tbr/assets/incomeToday?coinType='+arg
    this.common.get(api).then((res:any)=>{

      if(res.code == 200){
        if(arg == 'fil'){
          this.incometoday.fil = res.data
        }
        if(arg == 'xch'){
          this.incometoday.xch = res.data
        }
        if(arg == 'bzz'){
          this.incometoday.bzz = res.data
        }
      }
    })
  }

  // 矿池
  getPoolData(arg){
    var api = '/tbr/index/getPoolData?coinType='+arg
    this.common.get(api).then((res:any)=>{
      console.log(res)
      if(res.code == 200){
        if(arg == 'fil'){
          this.fildetails = res.data
        }
        if(arg == 'xch'){
          this.xchdetails = res.data
        }
        if(arg == 'bzz'){
          this.bzzdetails = res.data
        }
      }
    })
  }

  // 实名认证
  onWithdraw(){
    if(this.userInfo.personalStatus == 0){
      this.common.presentAlertUrl("您还未完成实名认证 <br /> 请完成实名认证后再进行提现",'/mine/idcenter')
    }else{
      this.router.navigate(['/withdraw'])
    }
  }

  onqb(){
    if(this.isqbfil == ''){
      this.common.presentAlertUrl("您还未绑定fil钱包 <br /> 请绑定钱包后再进行提币",'/mine/wallet')
      return false
    }
    this.router.navigate(['/qbtb'])
  }
  onqba(){
    if(this.isqbxch == ''){
      this.common.presentAlertUrl("您还未绑定xch钱包 <br /> 请绑定钱包后再进行提币",'/mine/wallet')
      return false
    }
    this.router.navigate(['/xchtb'])
  }

  // 判断是否绑定钱包
  isqb(){
    var api = '/tbr/wallet/validWallet?coinType=fil'
    this.common.get(api).then((res:any)=>{
      // console.log(res)
      if(res.code == 200){
          this.isqbfil = res.data.fil
          this.onqb()
      }
    })
  }

  isqba(){
    var api = '/tbr/wallet/validWallet?coinType=xch'
    this.common.get(api).then((res:any)=>{
      console.log(res)
      if(res.code == 200){
          this.isqbxch = res.data.xch
          this.onqba()
      }
    })
  }
  

}
